package com.dhnsoft.map;

import com.dhnsoft.game.Bullet;
import com.dhnsoft.util.BulletsPool;
import com.dhnsoft.util.Constant;
import com.dhnsoft.util.MyUtil;

import java.awt.*;
import java.util.List;

/**
 * @Description
 * @ClassName MapTile
 * @Author dhn
 * @date 2020.11.20 16:27
 */
//地图元素块
public class MapTile {
    public static final int TYPE_NORMAL = 0;
    public static final int TYPE_HOUSE = 1;
    public static final int TYPE_COVER= 2;
    public static final int TYPE_HARD= 3;
    public static final int BUFF_ATK = 1;
    public static int tileW = 40;
    public static int radius = tileW>>1;
    private int type = TYPE_NORMAL;
    private static Image[] tileImg;
    private static Image[] buffImg;

    static {
        tileImg = new Image[4];
        tileImg[TYPE_NORMAL] = MyUtil.createImage("images/tile.png");
        tileImg[TYPE_HOUSE] = MyUtil.createImage("images/house.png");
        tileImg[TYPE_COVER] = MyUtil.createImage("images/cover.png");
        tileImg[TYPE_HARD] = MyUtil.createImage("images/hard.png");
        buffImg = new Image[4];
        buffImg[BUFF_ATK]=MyUtil.createImage("images/atk.gif");
        if(tileW<=0){
            tileW = tileImg[TYPE_NORMAL].getWidth(null);
        }
    }
    //图片资源的左上角
    private int x,y;
    private boolean visible = true;

    public MapTile(int x, int y) {
        this.x = x;
        this.y = y;
        if(tileW<=0){
            tileW = tileImg[TYPE_NORMAL].getWidth(null);
        }
    }

    public MapTile() {

    }

    public void draw(Graphics g){
        if(!visible){
            return;
        }
        if(tileW<=0){
            tileW = tileImg[TYPE_NORMAL].getWidth(null);
        }
        g.drawImage(tileImg[type],x,y,null);
    }
    public void drawATK(Graphics g){
        if(!visible){
            return;
        }

        g.drawImage(buffImg[BUFF_ATK],425,230,null);
    }



    public int getX() {
        return x;
    }

    public void setX(int x) {
        this.x = x;
    }

    public int getY() {
        return y;
    }

    public void setY(int y) {
        this.y = y;
    }

    public boolean isVisible() {
        return visible;
    }

    public void setVisible(boolean visible) {
        this.visible = visible;
    }

    //判断当前的地图块是不是老巢
    public boolean isHouse(){
        return type == TYPE_HOUSE;
    }

    public int getType() {
        return type;
    }

    public void setType(int type) {
        this.type = type;
    }


    //地图块和子弹是否有碰撞
    public boolean isCollideBullet(List<Bullet> bullet){
        if(!visible || type == TYPE_COVER){
            return false;
        }
        for (Bullet bullet1 : bullet) {
            int bulletX = bullet1.getX();
            int bulletY = bullet1.getY();
            boolean collide = MyUtil.isCollide(x + radius, y + radius, radius, bulletX, bulletY);
            if(collide){
                //子弹的销毁 TODO
                bullet1.setVisible(false);
                BulletsPool.theReturn(bullet1);
                return true;
            }
        }
        return false;
    }

}


